home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH1 / SRC / STYLES.FRM < prev    next >
Text File  |  1996-05-04  |  14KB  |  473 lines

  1. VERSION 4.00
  2. Begin VB.Form StyleForm 
  3.    Caption         =   "Styles"
  4.    ClientHeight    =   4245
  5.    ClientLeft      =   825
  6.    ClientTop       =   1740
  7.    ClientWidth     =   7935
  8.    Height          =   4935
  9.    Left            =   765
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4245
  12.    ScaleWidth      =   7935
  13.    Top             =   1110
  14.    Width           =   8055
  15.    Begin VB.Frame Frame1 
  16.       Caption         =   "FillStyle"
  17.       Height          =   3135
  18.       Index           =   2
  19.       Left            =   1800
  20.       TabIndex        =   15
  21.       Top             =   1080
  22.       Width           =   2055
  23.       Begin VB.OptionButton FillStyleChoice 
  24.          Caption         =   "vbDiagonalCross"
  25.          Height          =   255
  26.          Index           =   7
  27.          Left            =   120
  28.          TabIndex        =   23
  29.          Top             =   2760
  30.          Width           =   1850
  31.       End
  32.       Begin VB.OptionButton FillStyleChoice 
  33.          Caption         =   "vbSolid"
  34.          Height          =   255
  35.          Index           =   0
  36.          Left            =   120
  37.          TabIndex        =   22
  38.          Top             =   240
  39.          Width           =   1850
  40.       End
  41.       Begin VB.OptionButton FillStyleChoice 
  42.          Caption         =   "(Transparent)"
  43.          Height          =   255
  44.          Index           =   1
  45.          Left            =   120
  46.          TabIndex        =   21
  47.          Top             =   600
  48.          Value           =   -1  'True
  49.          Width           =   1850
  50.       End
  51.       Begin VB.OptionButton FillStyleChoice 
  52.          Caption         =   "vbHorizontalLine"
  53.          Height          =   255
  54.          Index           =   2
  55.          Left            =   120
  56.          TabIndex        =   20
  57.          Top             =   960
  58.          Width           =   1850
  59.       End
  60.       Begin VB.OptionButton FillStyleChoice 
  61.          Caption         =   "vbVerticalLine"
  62.          Height          =   255
  63.          Index           =   3
  64.          Left            =   120
  65.          TabIndex        =   19
  66.          Top             =   1320
  67.          Width           =   1850
  68.       End
  69.       Begin VB.OptionButton FillStyleChoice 
  70.          Caption         =   "vbUpwardDiagonal"
  71.          Height          =   255
  72.          Index           =   4
  73.          Left            =   120
  74.          TabIndex        =   18
  75.          Top             =   1680
  76.          Width           =   1850
  77.       End
  78.       Begin VB.OptionButton FillStyleChoice 
  79.          Caption         =   "vbCross"
  80.          Height          =   255
  81.          Index           =   6
  82.          Left            =   120
  83.          TabIndex        =   16
  84.          Top             =   2400
  85.          Width           =   1850
  86.       End
  87.       Begin VB.OptionButton FillStyleChoice 
  88.          Caption         =   "vbDownwardDiagonal"
  89.          Height          =   255
  90.          Index           =   5
  91.          Left            =   120
  92.          TabIndex        =   17
  93.          Top             =   2040
  94.          Width           =   1910
  95.       End
  96.    End
  97.    Begin VB.TextBox WidthText 
  98.       Height          =   285
  99.       Left            =   1920
  100.       MaxLength       =   1
  101.       TabIndex        =   14
  102.       Text            =   "1"
  103.       Top             =   720
  104.       Width           =   375
  105.    End
  106.    Begin VB.Frame Frame1 
  107.       Caption         =   "DrawStyle"
  108.       Height          =   3135
  109.       Index           =   1
  110.       Left            =   0
  111.       TabIndex        =   2
  112.       Top             =   1080
  113.       Width           =   1695
  114.       Begin VB.OptionButton DrawStyleChoice 
  115.          Caption         =   "vbInsideSolid"
  116.          Height          =   255
  117.          Index           =   6
  118.          Left            =   120
  119.          TabIndex        =   13
  120.          Top             =   2400
  121.          Width           =   1455
  122.       End
  123.       Begin VB.OptionButton DrawStyleChoice 
  124.          Caption         =   "(Transparent)"
  125.          Height          =   255
  126.          Index           =   5
  127.          Left            =   120
  128.          TabIndex        =   12
  129.          Top             =   2040
  130.          Width           =   1455
  131.       End
  132.       Begin VB.OptionButton DrawStyleChoice 
  133.          Caption         =   "vbDashDotDot"
  134.          Height          =   255
  135.          Index           =   4
  136.          Left            =   120
  137.          TabIndex        =   11
  138.          Top             =   1680
  139.          Width           =   1455
  140.       End
  141.       Begin VB.OptionButton DrawStyleChoice 
  142.          Caption         =   "vbDashDot"
  143.          Height          =   255
  144.          Index           =   3
  145.          Left            =   120
  146.          TabIndex        =   10
  147.          Top             =   1320
  148.          Width           =   1455
  149.       End
  150.       Begin VB.OptionButton DrawStyleChoice 
  151.          Caption         =   "vbDot"
  152.          Height          =   255
  153.          Index           =   2
  154.          Left            =   120
  155.          TabIndex        =   9
  156.          Top             =   960
  157.          Width           =   1455
  158.       End
  159.       Begin VB.OptionButton DrawStyleChoice 
  160.          Caption         =   "vbDash"
  161.          Height          =   255
  162.          Index           =   1
  163.          Left            =   120
  164.          TabIndex        =   8
  165.          Top             =   600
  166.          Width           =   1455
  167.       End
  168.       Begin VB.OptionButton DrawStyleChoice 
  169.          Caption         =   "vbSolid"
  170.          Height          =   255
  171.          Index           =   0
  172.          Left            =   120
  173.          TabIndex        =   7
  174.          Top             =   240
  175.          Value           =   -1  'True
  176.          Width           =   1455
  177.       End
  178.    End
  179.    Begin VB.Frame Frame1 
  180.       Caption         =   "Object"
  181.       Height          =   615
  182.       Index           =   0
  183.       Left            =   0
  184.       TabIndex        =   1
  185.       Top             =   0
  186.       Width           =   3855
  187.       Begin VB.OptionButton ObjectChoice 
  188.          Caption         =   "Point"
  189.          Height          =   255
  190.          Index           =   3
  191.          Left            =   2880
  192.          TabIndex        =   24
  193.          Top             =   240
  194.          Width           =   735
  195.       End
  196.       Begin VB.OptionButton ObjectChoice 
  197.          Caption         =   "Box"
  198.          Height          =   255
  199.          Index           =   1
  200.          Left            =   1200
  201.          TabIndex        =   6
  202.          Top             =   240
  203.          Width           =   615
  204.       End
  205.       Begin VB.OptionButton ObjectChoice 
  206.          Caption         =   "Line"
  207.          Height          =   255
  208.          Index           =   0
  209.          Left            =   360
  210.          TabIndex        =   5
  211.          Top             =   240
  212.          Value           =   -1  'True
  213.          Width           =   735
  214.       End
  215.       Begin VB.OptionButton ObjectChoice 
  216.          Caption         =   "Circle"
  217.          Height          =   255
  218.          Index           =   2
  219.          Left            =   2040
  220.          TabIndex        =   4
  221.          Top             =   240
  222.          Width           =   735
  223.       End
  224.    End
  225.    Begin VB.PictureBox Canvas 
  226.       AutoRedraw      =   -1  'True
  227.       Height          =   4215
  228.       Left            =   3960
  229.       ScaleHeight     =   4155
  230.       ScaleWidth      =   3915
  231.       TabIndex        =   0
  232.       Top             =   0
  233.       Width           =   3975
  234.    End
  235.    Begin VB.Label Label1 
  236.       Caption         =   "DrawWidth"
  237.       Height          =   255
  238.       Left            =   1080
  239.       TabIndex        =   3
  240.       Top             =   750
  241.       Width           =   855
  242.    End
  243.    Begin VB.Menu mnuFile 
  244.       Caption         =   "&File"
  245.       Begin VB.Menu mnuFileExit 
  246.          Caption         =   "E&xit"
  247.       End
  248.    End
  249. End
  250. Attribute VB_Name = "StyleForm"
  251. Attribute VB_Creatable = False
  252. Attribute VB_Exposed = False
  253. Option Explicit
  254.  
  255. Const OBJ_LINE = 0
  256. Const OBJ_BOX = 1
  257. Const OBJ_CIRCLE = 2
  258. Const OBJ_POINT = 3
  259.  
  260. Dim Obj As Integer  ' The kind of object to draw.
  261.  
  262. Dim Rubberbanding As Boolean
  263. Dim OldMode As Integer
  264. Dim OldStyle As Integer
  265. Dim FirstX As Single
  266. Dim FirstY As Single
  267. Dim LastX As Single
  268. Dim LastY As Single
  269.  
  270.  
  271.  
  272. ' ***********************************************
  273. ' Draw the final (non-rubberband) object.
  274. ' ***********************************************
  275. Sub DrawObject()
  276.     ' Pick a random fill color.
  277.     Canvas.FillColor = QBColor(Int(Rnd * 16))
  278.     
  279.     ' Draw the object.
  280.     Select Case Obj
  281.         Case OBJ_LINE
  282.             Canvas.Line (FirstX, FirstY)-(LastX, LastY)
  283.         
  284.         Case OBJ_BOX
  285.             Canvas.Line (FirstX, FirstY)-(LastX, LastY), , B
  286.         
  287.         Case OBJ_CIRCLE
  288.             Dim xmid As Single
  289.             Dim ymid As Single
  290.             Dim dx As Single
  291.             Dim dy As Single
  292.             Dim radius As Single
  293.             
  294.             xmid = (FirstX + LastX) / 2
  295.             ymid = (FirstY + LastY) / 2
  296.             dx = Abs(FirstX - LastX)
  297.             dy = Abs(FirstY - LastY)
  298.             If dx < dy Then
  299.                 radius = dx / 2
  300.             Else
  301.                 radius = dy / 2
  302.             End If
  303.             Canvas.Circle (xmid, ymid), radius
  304.     
  305.         Case OBJ_POINT
  306.             Canvas.PSet (LastX, LastY)
  307.             
  308.     End Select
  309. End Sub
  310.  
  311. ' ***********************************************
  312. ' Draw the appropriate kind of rubberband object.
  313. ' ***********************************************
  314. Sub DrawRubberObject()
  315.     Select Case Obj
  316.         Case OBJ_LINE
  317.             Canvas.Line (FirstX, FirstY)-(LastX, LastY)
  318.         
  319.         Case OBJ_BOX
  320.             Canvas.Line (FirstX, FirstY)-(LastX, LastY), , B
  321.         
  322.         Case OBJ_CIRCLE
  323.             Dim xmid As Single
  324.             Dim ymid As Single
  325.             Dim dx As Single
  326.             Dim dy As Single
  327.             Dim radius As Single
  328.             
  329.             xmid = (FirstX + LastX) / 2
  330.             ymid = (FirstY + LastY) / 2
  331.             dx = Abs(FirstX - LastX)
  332.             dy = Abs(FirstY - LastY)
  333.             If dx < dy Then
  334.                 radius = dx / 2
  335.             Else
  336.                 radius = dy / 2
  337.             End If
  338.             Canvas.Circle (xmid, ymid), radius
  339.     
  340.         Case OBJ_POINT
  341.             Canvas.PSet (LastX, LastY)
  342.     
  343.     End Select
  344. End Sub
  345.  
  346.  
  347. Private Sub DrawStyleChoice_Click(Index As Integer)
  348.     Canvas.DrawStyle = Index
  349. End Sub
  350.  
  351. Private Sub FillStyleChoice_Click(Index As Integer)
  352.     Canvas.FillStyle = Index
  353. End Sub
  354.  
  355.  
  356. ' ***********************************************
  357. ' Start a rubberbanding of some sort.
  358. ' ***********************************************
  359. Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  360.     ' Let MouseMove know we are rubberbanding.
  361.     Rubberbanding = True
  362.     
  363.     ' Save values so we can restore them later.
  364.     OldMode = Canvas.DrawMode
  365.     OldStyle = Canvas.DrawStyle
  366.     Canvas.DrawMode = vbInvert
  367.     If Obj = OBJ_LINE Then
  368.         Canvas.DrawStyle = vbSolid
  369.     Else
  370.         Canvas.DrawStyle = vbDot
  371.     End If
  372.  
  373.     ' Save the starting coordinates.
  374.     FirstX = X
  375.     FirstY = Y
  376.     
  377.     ' Save the ending coordinates.
  378.     LastX = X
  379.     LastY = Y
  380.     
  381.     ' Draw the appropriate rubberband object.
  382.     DrawRubberObject
  383. End Sub
  384.  
  385.  
  386. ' ***********************************************
  387. ' Continue rubberbanding.
  388. ' ***********************************************
  389. Private Sub Canvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  390.     ' If we are not rubberbanding, do nothing.
  391.     If Not Rubberbanding Then Exit Sub
  392.     
  393.     ' Erase the previous rubberband object.
  394.     DrawRubberObject
  395.  
  396.     ' Save the new ending coordinates.
  397.     LastX = X
  398.     LastY = Y
  399.     
  400.     ' Draw the new rubberband object.
  401.     DrawRubberObject
  402. End Sub
  403.  
  404. ' ***********************************************
  405. ' Finish rubberbanding and draw the object.
  406. ' ***********************************************
  407. Private Sub Canvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  408.     ' If we are not rubberbanding, do nothing.
  409.     If Not Rubberbanding Then Exit Sub
  410.     
  411.     ' We are no longer rubberbanding.
  412.     Rubberbanding = False
  413.     
  414.     ' Erase the previous rubberband object.
  415.     DrawRubberObject
  416.     
  417.     ' Restore the original DrawMode and DrawStyle.
  418.     Canvas.DrawMode = OldMode
  419.     Canvas.DrawStyle = OldStyle
  420.  
  421.     ' Draw the final object.
  422.     DrawObject
  423. End Sub
  424.  
  425.  
  426. Private Sub Form_Load()
  427.     ' Select the default options.
  428.     DrawStyleChoice(Canvas.DrawStyle).Value = True
  429.     FillStyleChoice(Canvas.FillStyle).Value = True
  430.     ObjectChoice(Obj).Value = True
  431.     WidthText.Text = Format$(Canvas.DrawWidth)
  432. End Sub
  433.  
  434. Private Sub mnuFileExit_Click()
  435.     Unload Me
  436. End Sub
  437.  
  438. ' ***********************************************
  439. ' Record the kind of object to draw next.
  440. ' ***********************************************
  441. Private Sub ObjectChoice_Click(Index As Integer)
  442.     Obj = Index
  443. End Sub
  444.  
  445.  
  446. ' ***********************************************
  447. ' Change set DrawWidth.
  448. ' ***********************************************
  449. Private Sub WidthText_Change()
  450. Dim wid As Integer
  451.  
  452.     If Not IsNumeric(WidthText.Text) Then Exit Sub
  453.     
  454.     wid = CInt(WidthText.Text)
  455.     If wid < 1 Then Exit Sub
  456.     
  457.     Canvas.DrawWidth = wid
  458. End Sub
  459.  
  460. ' ***********************************************
  461. ' Only allow 1 through 9.
  462. ' ***********************************************
  463. Private Sub WidthText_KeyPress(KeyAscii As Integer)
  464.     If KeyAscii < Asc(" ") Or _
  465.        KeyAscii > Asc("~") Then Exit Sub
  466.     If KeyAscii >= Asc("1") And _
  467.        KeyAscii <= Asc("9") Then Exit Sub
  468.     Beep
  469.     KeyAscii = 0
  470. End Sub
  471.  
  472.  
  473.